#include <iostream>
using namespace std;

void print1D(int* a, int n){
    for (int i=0; i<n; ++i){
        cout << a[i] << " ";
    }
    cout << endl;
}

void TESTsizeof(){
    printf("\n================== %s ===================\n", __func__);
    auto sizeoffunc = [](int a[]){
        cout << "数组传入函数后，sizeof退化为指针的大小" << endl;
        cout << "sizeof(a1) = " << sizeof(a) << endl;
    };

    int a1[10] = {1};

    cout << "在同一域中，sizeof给出整个数组的大小" << endl;
    cout << "sizeof(a1) = " << sizeof(a1) << endl;
    sizeoffunc(a1); 
}

void TESTinit(){
    printf("\n================== %s ===================\n", __func__);
    int a1[10] = {1};
    int a2[10] = {2,2,2};
    int a3[10]{3};
    print1D(a1, 10);
    print1D(a2, 10);
    print1D(a3, 10);
}

void TESTbound(){
    printf("\n================== %s ===================\n", __func__);
    int a1[10] = {0};
    cout << "数组越界程序可能并不会崩溃，因此需要特别注意" << endl;
    print1D(a1, 11);
    cout << a1[-1] << endl;
}

void print2D(int* a, int m, int n){
    cout << "[";
    for (int i=0; i<m; ++i){
        cout << "[";
        for (int j=0; j<n; ++j){
            cout << a[i*n + j];
            if (j < n-1) cout << ",";
            else cout << "]";
        }
        if (i < m-1) cout << ",";
        else cout << "]";
        cout << endl;
    }
}

void TESTinit2D(){
    printf("\n================== %s ===================\n", __func__);
    int a1[3][4] = {0};
    int a2[3][4] = {2,2,2,0,0};
    int a3[3][4] = {3,3,3,3,3,3,3};
    int a4[3][4] = {{4,4,4,4}, {4,4,4,4}};
    cout << "a1 = "; print2D((int*)a1, 3, 4);
    cout << "a2 = "; print2D((int*)a2, 3, 4);
    cout << "a3 = "; print2D((int*)a3, 3, 4);
    cout << "a4 = "; print2D((int*)a4, 3, 4);
}

void print3D(int* a, int d1, int d2, int d3){
    cout << "[";
    for (int i=0; i<d1; ++i){
        cout << "[";
        for (int j=0; j<d2; ++j){
            cout << "[";
            for (int k=0; k<d3; ++k){
                cout << a[i*d2*d3 + j*d3 + k];
                if (k < d3-1) cout << ",";
                else cout << "]";
            }
            if (j < d2-1) cout << ",";
            else cout << "]";
        }
        if (i < d1-1) cout << ",";
        else cout << "]";
        cout << endl;
    }
}

void TESTinit3D(){
    printf("\n================== %s ===================\n", __func__);
    int a1[2][3][4] = {1,2,3,4,5,6,7,8,9,10,11,12,
                       13,14,15,16,17,18,19,20,21,22,23,24};
    cout << "a1 = "; print3D((int*)a1, 2, 3, 4);
}

void TESTpointer(){
    printf("\n================== %s ===================\n", __func__);
    auto pdiff = [](int* a, int* b){
        return a-b;
    };
    int a1[2][3][4];
    cout << a1+1 << "-" << a1 << "=" << pdiff((int*)(a1+1), (int*)a1) << endl;;
    cout << a1[0]+1 << "-" << a1[0] << "="  << pdiff((int*)(a1[0]+1), (int*)a1[0]) << endl;;
    cout << a1[0][0]+1 << "-" << a1[0][0] << "="  << pdiff((int*)(a1[0][0]+1), (int*)a1[0][0]) << endl;;
}

class Obj{
public:
    Obj(){
        cout << __func__ << endl;
    };

    ~Obj(){
        cout << __func__ << endl;
    }
};

void TESTnew(){
    printf("\n================== %s ===================\n", __func__);
    cout << "new一个对象数组时，会调用n次构造函数，delete它时则会同样调用n次析构函数" << endl;
    Obj* p1 = new Obj[3];
    delete[] p1;
    // new[] 与 delete[]需要对应，下面这行代码会导致 segmetation fault
    //delete p1;
}

int main(){
    TESTsizeof();
    TESTinit();
    TESTbound();
    TESTinit2D();
    TESTinit3D();
    TESTpointer();
    TESTnew();
    return 0;
}
